package cn.dnui_wwt524.dnuifood_16110100524.controller;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.List;

import cn.dnui_wwt524.dnuifood_16110100524.R;
import cn.dnui_wwt524.dnuifood_16110100524.bean.MeBean;
import cn.dnui_wwt524.dnuifood_16110100524.listener.OnRetrofitListener;
import cn.dnui_wwt524.dnuifood_16110100524.model.MeModel;

public class UserInfoActivity extends BaseActivity implements OnRetrofitListener<MeBean> {
private Button me_button;
private EditText me_username,me_password,me_phone,me_address;
String userid;
    @Override
    void initViews() {
        layout_file =R.layout.activity_user_info;
        setLayout(layout_file);
        me_username = findViewById(R.id.me_username);
        me_password = findViewById(R.id.me_password);
        me_phone = findViewById(R.id.me_phone);
        me_address = findViewById(R.id.me_address);
        me_button = findViewById(R.id.me_button);
    }

    @Override
    void initEvents() {
        me_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                me_update();
            }
        });
    }

    @Override
    void initDate() {
        MeModel meModel=new MeModel();
        meModel.getUserById(userid,this);
    }
    void me_update(){
        MeModel meModel=new MeModel();
        meModel.updateUserById(userid,
                me_username.getText().toString(),
                me_password.getText().toString(),
                me_phone.getText().toString(),
                me_address.getText().toString(),this);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        SharedPreferences sharedPreferences = getSharedPreferences("userinfo", MODE_PRIVATE);
        userid = (String)sharedPreferences.getString("userid", "");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        initViews();
        initDate();
        initEvents();
    }

    @Override
    public void onSuccess(MeBean object, int flag) {
        switch (flag){
            case 1:
                me_username.setText(object.getUsername());
                me_password.setText(object.getUserpass());
                me_phone.setText(object.getMobilenum());
                me_address.setText(object.getAddress());
                Toast.makeText(this, userid, Toast.LENGTH_SHORT).show();
                break;
            case 2:
                if(object.getSuccess().equals("1")){
                    Toast.makeText(UserInfoActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(UserInfoActivity.this,"修改失败",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(UserInfoActivity.this,"联网失败",Toast.LENGTH_SHORT).show();
    }
}
